package com.jinsi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.jinsi.mapper.ScStandard;
import com.jinsi.pojo.*;
import com.jinsi.pojo.topo.ScStandardInfo;
import com.jinsi.service.ScStandardService;
import com.jinsi.utils.MapReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Date : 2022/6/29 14:59
 * 小南不难
 **/
@Service
public class ScStandardServiceImpl implements ScStandardService {
    //    导入mapper
    @Autowired
    private ScStandard scStandard;

    @Override
    public List<ScStudentStandard> getScStandardBySid2(String sid) {
        try {
            List<ScStudentStandard> list = scStandard.getScStandardBySid2(sid);
            return list;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e);
        }
        return null;
    }



    @Override
    public Map<String, Object> updateStandardById(int id, BigDecimal preferentialPrice, String remark) {
        //根据标准id查询原有数据
        try {
            return MapReturn.getSuccess( scStandard.updateStandardById(id,preferentialPrice,remark));
        }catch (Exception e){
            System.out.println("数据查询有问题========?"+e.getMessage());
            return MapReturn.getError("查询失败");
        }
    }

    @Override
    public List<ScStudentStandard> getScStandardBySid(String sid,Boolean isArrarage) {
        try {
            List<ScStudentStandard> list = scStandard.getScStandardBySid(sid,isArrarage,null);
            return list;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e);
        }
        return null;
    }

    @Override
    public List<ScStudentStandard> getScStandardBySid(JSONArray jsonArray) {
        try {
            List<ScStudentStandard> list = scStandard.getScStandardBySid(null,false,jsonArray);
            return list;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e);
        }
        return null;
    }

    //获取用户信息
    @Override
    public ScStandardInfo getStudentInfo(String info) {
        try {
            ScStandardInfo studentInfo = scStandard.getStudentInfo(info);
            iptStandardLog(4, "查询用户信息", "200");
            return studentInfo;
        } catch (Exception e) {
            iptStandardLog(4, "查询用户信息", "500");
            System.out.println("数据库有些问题=========>" + e);
            return null;
        }
    }

    @Override
    public List<ScProjectList> getProjectList() {
        try {
            List<ScProjectList> projectList = scStandard.getProjectList();
            return projectList;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e);
        }
        return null;
    }

    @Override
    public int iptProject(JSONArray jsonArray) {
        try {
            int i = scStandard.iptProject(jsonArray);
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
        }
        return 0;
    }

    //修改项目信息
    @Override
    public int uptProject(ScStudentStandard studentStandard) {
        try {
            int i = scStandard.uptProject(studentStandard);
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
        }
        return 0;
    }

    @Override
    public int delProject(int id) {
        try {
            int i = scStandard.delProject(id);
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
        }
        return 0;
    }

    @Override
    public List<ScRates> getDormitory(int inherit) {
        try {
            List<ScRates> list = scStandard.getDormitory(inherit);
            return list;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            return null;
        }
    }

    @Override
    public int iptRates(ScRates scRates) {
        try {
            int i = scStandard.iptRates(scRates);
            //更新宿舍标准
             scStandard.updateProject();
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            return -1;
        }
    }

    @Override
    public int delRates(int id) {
        try {
            scStandard.uptProjectPrice(id); //刷新总价钱
            int i = scStandard.delRates(id);
            iptStandardLog(2, "删除项目标准", "200");
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(2, "删除项目标准", "500");
            return -1;
        }
    }

    @Override
    public int uptRates(ScRates scRates) {
        //修改项目标准，同时更新总价
        try {
            scStandard.uptProjectPrice(scRates.getId()); //刷新总价钱
            int i = scStandard.uptRates(scRates);
            iptStandardLog(3, "修改项目标准", "200");
            scStandard.updateProject();
            return i;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(3, "修改项目标准", "500");
            return -1;
        }
    }

    //查看所有班级收费标准
    @Override
    public List<ScClassesStandard> getClassesStandard(String pastDue, String startTime) {
        try {
            List<ScClassesStandard> list = scStandard.getClassesStandard(pastDue, startTime);
            iptStandardLog(4, "查看所有班级收费标准", "200");
            return list;
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(4, "查看所有班级收费标准", "500");
            return null;
        }
    }

    //学费收费标准添加
    @Override
    public Map<String, Object> istClassesPrice(ScClassesStandard scClassesStandard) {
        try {
            int i = scStandard.istClassesPrice(scClassesStandard);
            if (i == 1) {
                iptStandardLog(1, "添加学费收费标准", "200");
                return MapReturn.getSuccess("添加成功");
            }
            iptStandardLog(1, "添加学费收费标准", "500");
            return MapReturn.getError("数据插入失败");
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(1, "添加学费收费标准", "500");
            return MapReturn.getError("服务异常");
        }
    }

    //修改班级标准金额
    @Override
    public Map<String, Object> uptClassesMoney(ScClassesStandard scClassesStandard) {
        try {
            int i = scStandard.uptClassesMoney(scClassesStandard);
            if (i == 1) {
                iptStandardLog(3, "修改班级标准金额", "200");
                return MapReturn.getSuccess("修改成功");
            }
            iptStandardLog(3, "修改班级标准金额", "500");
            return MapReturn.getError("修改失败");
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(3, "修改班级标准金额", "500");
            return MapReturn.getError("服务器异常");
        }
    }

    //修改学费启用状态
    @Override
    public Map<String, Object> uptClassesStartUseing(ScClassesStandard scClassesStandard) {
        try {
            int i = scStandard.uptClassesStartUseing(scClassesStandard);
            if (i == 1) {
                iptStandardLog(3, "修改学费启用状态", "200");
                return MapReturn.getSuccess("修改成功");
            }
            iptStandardLog(3, "删除学费收费标准", "500");
            return MapReturn.getError("数据修改失败");
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(3, "删除学费收费标准", "500");
            return MapReturn.getError("服务异常");
        }
    }

    //删除学费收取标准
    @Override
    public Map<String, Object> delClasserStandard(int id) {
        try {
            int i = scStandard.delClasserStandard(id);
            if (i == 1) {
                iptStandardLog(2, "删除学费收费标准", "200");
                return MapReturn.getSuccess("删除成功");
            }
            iptStandardLog(2, "删除学费收费标准", "500");
            return MapReturn.getError("数据删除失败");
        } catch (Exception e) {
            System.out.println("数据库有些问题=========>" + e.getMessage());
            iptStandardLog(2, "删除学费收费标准", "500");
            return MapReturn.getError("服务异常");
        }
    }

    //日志增加监听
    @Override
    public void iptStandardLog(int type, String module, String state) {
        try {
            ScStandardLog scStandardLog = new ScStandardLog(type, module, state, new Date());
            scStandard.iptStandardLog(scStandardLog);
        } catch (Exception e) {
            System.out.println("日志添加失败=========>" + e.getMessage());
        }
    }

    //日志查询
    @Override
    public Map<String, Object> getStandardLog() {
        try {
            return MapReturn.getSuccess(scStandard.getStandardLog());
        } catch (Exception e) {
            System.out.println("日志查询失败=========>" + e.getMessage());
            return MapReturn.getError("日志查询失败");
        }
    }

    //日志删除
    public Map<String, Object> delStandardLog(int id) {
        try {
            if (scStandard.delStandardLog(id) == 1) {
                return MapReturn.getSuccess("删除成功");
            }
            return MapReturn.getError("日志删除失败");
        } catch (Exception e) {
            System.out.println("日志删除失败=========>" + e.getMessage());
            return MapReturn.getError("日志删除失败");
        }
    }
}
